//{$CLEO .lm}
{$CLEO}
//{$I asmdef.txt}
// tail.lm

const    
    // general
    VehicleHandle = 0@
    VehicleStruct = 1@
    UnicalNumberOfCorona = 25@
    RpClump = 18@
    RwFrame = 19@
    CurrentCarOfPlayer = 15@
    OldCarOfPlayer = 16@
    StatusOfPlayerCar = 17@
    CamX = 12@
    CamY = 13@
    CamZ = 14@
    DamageManager = 23@
    CarDriver = 24@
    Timer = 32@
    CarX = 6@
    CarY = 7@
    CarZ = 8@

    fR = 0.16
    uI = 128
    intensityA = 1
    distanceToGround = 0x40C00000
    colorR = 32
    colorG = 22
    colorB = 0
    
    char_pl = $player_char
    char_act = $player_actor
    
    HEADLIGHT_LEFT = 0
    HEADLIGHT_RIGHT = 1
    LIGHT_RIGHT = 2
    LIGHT_LEFT = 3

    _registerCorona = 0x6FC580
    _registerShadow = 0x707390
    _registerAmbientLight = 0x7000E0
    _getLightState = 0x6C2130 // int _getLightState(int eLight)
    _setLightState = 0x6C2100 // _setLightState(int eLight, int State)
    _getPanelState = 0x6C2180 // int _getPanelState(int ePanel)
    _RwFrame_GetLTM = 0x7F0990
    
    
    
end

// --> included to ImVehLights.asi 
//0A8C: 0x6E1996 4 0x858624 1 // change fZero to fOne
0A8C: 0x6E18E5 6 0x90 1   // camera stuff

//BACKLIGHT CORONAS // <- +2 values
0AC6: 0@ = label @values offset
0a8c: 0@ 4 120.0 0
0a8c: 0x6E19D2 4 0@ 1
0@ += 4
0a8c: 0@ 4 170.0 0
0a8c: 0x6E19B6 4 0@ 1
0@ += 4
0a8c: 0@ 4 0.4 0
0a8c: 0x6E1941 4 0@ 1
0a8c: 0x6E1A22 4 190 1

// added 12-02-2012
0A8C: 0x6E28E7 5 0x90 1 // removes original PointLight for Vehicle backlights (works weird)
// test RGB
//0x8D368C 12 bytes unused!

while true
    wait 0
    068D: get_camera_position_to 17@ 18@ 19@
    0A8D: 31@ = read_memory 0xB74494 size 4 virtual_protect 0
    000A: 31@ += 0x4
    0A8D: 31@ = read_memory 31@ size 4 virtual_protect 0
    for 30@ = 0 to 27904 step 0x100
        0A8D: 0@ = read_memory 31@ size 1 virtual_protect 0
        000A: 31@ += 0x1
        if and
            0029: 0@ >= 0x00 
            001B: 0x80 > 0@
        then 
            005A: 0@ += 30@
            // 0@ - handle of a car
            0A97: 1@ = car 0@ struct            
            0A8E: DamageManager = VehicleStruct + 0x5A0 
            car.StorePos(VehicleHandle, CarX, CarY, CarZ)
            046C: CarDriver = car VehicleHandle driver
            gosub @carlight
        end
    end
end

:carlight
0441: 20@ = car VehicleHandle model
if and //   -  ,  ,   ,  ,    
    881E: not model 20@ boat
    881F: not model 20@ plane
    8820: not model 20@ heli
    20@ <> #BIKE
    20@ <> #BMX
    20@ <> #MTBIKE
    0ABE: vehicle VehicleHandle engine_on
then
    0A8E: 8@ = VehicleStruct + 0x584
    0A8D: 8@ = read_memory 8@ size 4 virtual_protect 0
    if                                                              
        08B7: test 8@ bit LIGHT_RIGHT                                        
    then
        if
            08B7: test 8@ bit LIGHT_LEFT //    
        then                                                            
            gosub @create_2
        else                           
            gosub @create_right                                                                                                                   
        end                                                                 
    else                                                                    
        if
            08B7: test 8@ bit LIGHT_LEFT //    
        then                         
            gosub @create_left                                                                              
        else
            0A8E: 27@ = VehicleStruct + 0x4A0
            0A8D: 27@ = read_memory 27@ size 4 virtual_protext 0 //   
            if
                27@ == 1.0
            then
                if
                    0D0D: 27@ = get_car VehicleHandle component "breaklight_l" // IF and SET
                then
                    if
                        0D0D: 27@ = get_car VehicleHandle component "breaklight_r" // IF and SET
                    then    
                        0AA8: call_function_method _getLightState struct DamageManager num_params 1 pop 0 LIGHT_LEFT 20@
                        0AA8: call_function_method _getLightState struct DamageManager num_params 1 pop 0 LIGHT_RIGHT 21@
                        if and
                            20@ == 0
                            21@ == 0
                        then
                            0441: 20@ = car VehicleHandle model
                            gosub @create_2
                        else
                            if
                                20@ == 0
                            then
                                0441: 20@ = car VehicleHandle model
                                gosub @create_left
                            end
                        end
                    end
                else
                    if
                        0D0D: 27@ = get_car VehicleHandle component "breaklight_r" // IF and SET
                    then
                        0AA8: call_function_method _getLightState struct DamageManager num_params 1 pop 0 LIGHT_RIGHT 21@
                        if
                            21@ == 0
                        then
                            0441: 20@ = car VehicleHandle model
                            gosub @create_right
                        end
                    end
                end
            end
        end
    end
end
return

:create_2
mul(20@, 4)
inc(20@, 0xA9B0C8) //  CModelBase
0A8D: 20@ = read_memory 20@ size 4 virtual_protect 0                                              
inc(20@, 0x5C)
0A8D: 20@ = read_memory 20@ size 4 virtual_protect 0
inc(20@, 0xC)
0A8D: 2@ = read_memory 20@ size 4 virtual_protect 0 // X
inc(20@, 4)
0A8D: 3@ = read_memory 20@ size 4 virtual_protect 0 // Y
inc(20@, 4)
0A8D: 4@ = read_memory 20@ size 4 virtual_protect 0 // Z
//
0407: store_coords_to 5@ 6@ 7@ from_car VehicleHandle with_offset 0.0 3@ 4@
0087: 8@ = 3@
8@ -= 2.2
0407: store_coords_to 8@ 9@ 10@ from_car VehicleHandle with_offset 0.0 8@ 4@
0063: 8@ -= 5@
0063: 9@ -= 6@
0063: 10@ -= 7@
0AA5: call _registerAmbientLight num_params 14 pop 14 entity 0 unkb 0 unkb 1 Blue 0.0 Green 0.0 Red 0.5 Radius 2.5 10@ 9@ 8@ 7@ 6@ 5@ 1
//
0407: store_coords_to 6@ 7@ 11@ from_car VehicleHandle with_offset 2@ 3@ 4@
2@ *= 1.6
2@ += 1.8 // sizeX
3@ -= 2.5 //    Y      

0407: store_coords_to 5@ 8@ 27@ from_car VehicleHandle with_offset 0.0 2.4 0.0 // sizeY -  , Y
0407: store_coords_to 6@ 7@ 27@ from_car VehicleHandle with_offset 2@ 0.0 0.0
0407: store_coords_to 9@ 10@ 11@ from_car VehicleHandle with_offset 0.0 3@ 4@
0407: store_coords_to 20@ 21@ 27@ from_car VehicleHandle with_offset 0.0 0.0 0.0
{
0D0A: store_coords_to 5@ 8@ 27@ from_matrix VehicleHandle with_offset 0.0 2.4 0.0 // sizeY -  , Y
0D0A: store_coords_to 6@ 7@ 27@ from_matrix VehicleHandle with_offset 2@ 0.0 0.0
0D0A: store_coords_to 9@ 10@ 11@ from_matrix VehicleHandle with_offset 0.0 3@ 4@
0D0A: store_coords_to 20@ 21@ 27@ from_matrix VehicleHandle with_offset 0.0 0.0 0.0
}
gosub @create_shadow2
return

:create_right
mul(20@, 4)                                                                                           
inc(20@, 0xA9B0C8) //  CModelBase                                                             
0A8D: 20@ = read_memory 20@ size 4 virtual_protect false
inc(20@, 0x5C)
0A8D: 20@ = read_memory 20@ size 4 virtual_protect false
inc(20@, 0xC)                                           
0A8D: 2@ = read_memory 20@ size 4 virtual_protect false // X                                         
inc(20@, 4)                                                                                           
0A8D: 3@ = read_memory 20@ size 4 virtual_protect false // Y                                         
inc(20@, 4)                                                                                           
0A8D: 4@ = read_memory 20@ size 4 virtual_protect false // Z
//
0407: store_coords_to 5@ 6@ 7@ from_car VehicleHandle with_offset 2@ 3@ 4@
0087: 8@ = 3@
8@ -= 2.2
0407: store_coords_to 8@ 9@ 10@ from_car VehicleHandle with_offset 2@ 8@ 4@
0063: 8@ -= 5@
0063: 9@ -= 6@
0063: 10@ -= 7@
0AA5: call _registerAmbientLight num_params 14 pop 14 entity VehicleStruct unkb 0 unkb 1 Blue 0.0 Green 0.0 Red 0.5 Radius 1.25 10@ 9@ 8@ 7@ 6@ 5@ 1
//                                         
0407: store_coords_to 6@ 7@ 11@ from_car VehicleHandle with_offset 2@ 3@ 4@
3@ -= 2.5

0407: store_coords_to 5@ 8@ 27@ from_car VehicleHandle with_offset 0.0 2.4 0.0                             
0407: store_coords_to 6@ 7@ 27@ from_car VehicleHandle with_offset -1.8 0.0 0.0                                
0407: store_coords_to 20@ 21@ 27@ from_car VehicleHandle with_offset 0.0 0.0 0.0                                
0407: store_coords_to 9@ 10@ 11@ from_car VehicleHandle with_offset 2@ 3@ 4@
{
0D0A: store_coords_to 5@ 8@ 27@ from_matrix VehicleHandle with_offset 0.0 2.4 0.0                             
0D0A: store_coords_to 6@ 7@ 27@ from_matrix VehicleHandle with_offset -1.8 0.0 0.0                                
0D0A: store_coords_to 20@ 21@ 27@ from_matrix VehicleHandle with_offset 0.0 0.0 0.0                                
0D0A: store_coords_to 9@ 10@ 11@ from_matrix VehicleHandle with_offset 2@ 3@ 4@ 
}                                                                                                                                                                                                                                                                                                                                                                                
gosub @create_shadow2
return

:create_left
mul(20@, 4)                                                                                           
inc(20@, 0xA9B0C8) //  CModelBase                                                             
0A8D: 20@ = read_memory 20@ size 4 virtual_protect false                                              
inc(20@, 0x5C)                                                                                        
0A8D: 20@ = read_memory 20@ size 4 virtual_protect false                                              
inc(20@, 0xC)                                                                                         
0A8D: 2@ = read_memory 20@ size 4 virtual_protect false // X                                         
inc(20@, 4)                                                                                           
0A8D: 3@ = read_memory 20@ size 4 virtual_protect false // Y                                         
inc(20@, 4)                                                                                           
0A8D: 4@ = read_memory 20@ size 4 virtual_protect false // Z                                         
0407: store_coords_to 6@ 7@ 11@ from_car VehicleHandle with_offset 2@ 3@ 4@                                                                                                                                                                   
mul(2@, -1.0)
//
0407: store_coords_to 5@ 6@ 7@ from_car VehicleHandle with_offset 2@ 3@ 4@
0087: 8@ = 3@
8@ -= 2.2
0407: store_coords_to 8@ 9@ 10@ from_car VehicleHandle with_offset 2@ 8@ 4@
0063: 8@ -= 5@
0063: 9@ -= 6@
0063: 10@ -= 7@
0AA5: call _registerAmbientLight num_params 14 pop 14 entity VehicleStruct unkb 0 unkb 1 Blue 0.0 Green 0.0 Red 0.5 Radius 1.25 10@ 9@ 8@ 7@ 6@ 5@ 1
//
3@ -= 2.5
                                                                                         
0407: store_coords_to 5@ 8@ 27@ from_car VehicleHandle with_offset 0.0 2.4 0.0                               
0407: store_coords_to 6@ 7@ 27@ from_car VehicleHandle with_offset 1.8 0.0 0.0                                
0407: store_coords_to 20@ 21@ 27@ from_car VehicleHandle with_offset 0.0 0.0 0.0                                
0407: store_coords_to 9@ 10@ 11@ from_car VehicleHandle with_offset 2@ 3@ 4@
{
0D0A: store_coords_to 5@ 8@ 27@ from_matrix VehicleHandle with_offset 0.0 2.4 0.0                               
0D0A: store_coords_to 6@ 7@ 27@ from_matrix VehicleHandle with_offset 1.8 0.0 0.0                                
0D0A: store_coords_to 20@ 21@ 27@ from_matrix VehicleHandle with_offset 0.0 0.0 0.0                                
0D0A: store_coords_to 9@ 10@ 11@ from_matrix VehicleHandle with_offset 2@ 3@ 4@
}                                                                                                                                                                                                                                                                                                                                                                           
gosub @create_shadow2
return

:create_shadow2
0063: 5@ -= 20@                                                                                            
0063: 8@ -= 21@                                                                                            
0063: 6@ -= 20@                                                                                            
0063: 7@ -= 21@
// additional check //
0A8E: 27@ = VehicleStruct + 0x584
0A8D: 27@ = read_memory 27@ size 4 virtual_protect 0
if and                                                             
    88B7: not test 27@ bit 2
    88B7: not test 27@ bit 3
then
    27@ = 20.0
else
    0A8E: 27@ = VehicleStruct + 0x4A0
    0A8D: 27@ = read_memory 27@ size 4 virtual_protect 0 //   
    if
        27@ == 1.0
    then
        27@ = 70.0
    else
        27@ = 50.0
    end
end
0092: 27@ = float 27@ to_integer

0A8D: 4@ 0xC4040C 4 0
                
0AC7: 3@ = var 9@ offset
0AA5: call _registerShadow num_params 16 pop 16 0 0 0x3F800000 0 2.0 0 0 27@ 1 7@ 6@ 8@ 5@ 3@ 4@ 2
return

:values
hex
    00000000 00000000 00000000 00000000  00000000 00000000 00000000 00000000
end